package cn.shengchao.examstar.gankao.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.shengchao.examstar.rpc.gankao.GanKaoOrderDto;
import cn.shengchao.examstar.rpc.gankao.GanKaoOrderGoodsListDto;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: examstarapi
 * @description: 赶考网请求封装
 * @author: Xiong weiqiang
 * @create: 2025-06-23 17:24
 */
@Component
public class GanKaoHttpUtil {
    @Value("${third.gankao.serverUrl:https://www.gankao.com/api/service}")
    private String serverUrl;
    @Value("${third.gankao.appId:yunshi}")
    private String appId;
    @Value("${third.gankao.appSecret:e4cd808c-4a79-11f0-bca4-b8599fe54952}")
    private String appSecret;
    public void testUrl(){
        System.out.println("serverUrl="+serverUrl);
    }
    /**
     * 赶考网请求
     *
     * @param uri
     * @param params
     * @return
     */
    public String requestApi(String uri, Map<String, Object> params) {
        return GanKaoHttpRequest.requestApi(serverUrl, uri, params, appId, appSecret);
    }

    public String requestBuySection(@NotNull String uid,@NotNull  String orderId, @NotNull  BigDecimal orderAmount, @NotEmpty List<GanKaoOrderGoodsListDto> goodsList) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("app_id", appId);
//        params.put("phone", "");
        params.put("timestamp",System.currentTimeMillis());
        String token = GanKaoHttpParams.getSecretTokenByParams(params, appSecret);
        params.put("token", token);
        String url = serverUrl + GanKaoHttpParams.URI_BUY_SECTION_API;
        HttpRequest request = HttpUtil.createPost(url);
        GanKaoOrderDto orderDto = new GanKaoOrderDto();
        orderDto.setOrderId(orderId);
        orderDto.setOrderAmount(orderAmount);
        orderDto.setUid(uid);
        orderDto.setGoodsList(JSONUtil.toJsonStr(goodsList));
        JSONObject orderDtoJson = new JSONObject(orderDto);
        for (String key : params.keySet()) {
            Object value = params.get(key);
            orderDtoJson.set(key, value);
        }
        String body = orderDtoJson.toString();
        System.out.println("正在请求赶考网用户买课接口："+body);
        request.body(body);
        String result = request.execute().body();
        System.out.println("请求赶考网用户买课接口返回："+result);
        return result;

    }
    public String requestCreateUser(String uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        return requestApi(GanKaoHttpParams.URI_CREATE_USER_API, params);
    }
    public String requestGetUserSections(String uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        return requestApi(GanKaoHttpParams.URI_GET_USER_SECTIONS_API, params);
    }

    public String requestGetPlayVideoResourceForWeb(String uid, int courseId, int sectionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("course_id", courseId);
        params.put("section_id", sectionId);
        return requestApi(GanKaoHttpParams.URI_GET_PLAY_VIDEO_RESOURCE_FOR_WEB_API, params);
    }
}